package com.fingard.dsp.bank.directbank.gzrcb01;

import org.apache.commons.lang.time.DateFormatUtils;

import com.fingard.constant.Format;
import com.fingard.dsp.bank.directbank.DirectBase;
import com.fingard.dsp.bank.directbank.PaymentState;
import com.fingard.dsp.bank.directbank.bankInterface.ISinglePay;
import com.fingard.dsp.bank.directbank.fngrd01.util.StringHelper;
import com.fingard.dsp.bank.reqrespobj.single.*;
import com.fingard.dsp.bank.reqrespobj.single.detail.RetAtsSglDetail;
import com.fingard.net.ConnState;
import com.fingard.xml.XElement;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;

import java.text.ParseException;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by F on 2018/5/10.
 */
public class GZRCB01SinglePay extends GZRCB01Base implements ISinglePay{

    @Override
    public RetAtsSingle d_1908_SglPay(ReqSingle p_req1908) throws Exception {
        return p_req1908.singleDetail.sameBnk.equals("1")?payBetweenSameBanks(p_req1908):payBetweenDifferenceBanks(p_req1908);

    }

    @Override
    public RetAtsSingleBat d_1908BAT_SglBatPay(ReqSingleBat p_req1908BAT) throws Exception {
        return null;
    }

    @Override
    public RetAtsSingle d_1944_QrySglPay(ReqQrySingle p_req1944) throws Exception {
        RetAtsSingle retAtsSingle = new RetAtsSingle(p_req1944.reqHeader);
        RetAtsSglDetail detail = new RetAtsSglDetail();
        String opname = "YQ267";
        try{
            XmlTextWriter xmlTextWriter = new XmlTextWriter();
            GZRCB01Header header = new GZRCB01Header();
            header.chnl_dt = Format.Date8Format.format(Format.DateTimeFormat.parse(p_req1944.reqHeader.transTime));
            header.chnl_tm = Format.Time6Format.format(Format.DateTimeFormat.parse(p_req1944.reqHeader.transTime));
            header.chnl_seq =p_req1944.reqHeader.transSeqID ;
            wirteCommenHeader(xmlTextWriter,header);
            XElement main_data = new XElement("main_data","");
            main_data.addChild(new XElement("USERID",bankFront.loginName));//用户号
            String JIOYRQ = Format.Date8Format.format(Format.DateTimeFormat.parse(p_req1944.bgnDate));
            if(JIOYRQ.isEmpty()||p_req1944.askNumber.isEmpty()){
                detail.respCode ="-1";
                detail.respInfo = "查询时交易日期和客户流水号不能为空";
            }else {
                main_data.addChild(new XElement("JIOYRQ", JIOYRQ));//交易日期
                main_data.addChild(new XElement("QANTLS", p_req1944.askNumber));//客户流水号
                main_data.addChild(new XElement("BEIY01", ""));//备用字段1
                main_data.addChild(new XElement("BEIY02", ""));//备用字段2
                String plain = bankFront.loginName + "|" + JIOYRQ + "|" + p_req1944.askNumber;
                main_data.addChild(new XElement("SIGDAT", plain));
                xmlTextWriter.writeXElement(main_data);
                xmlTextWriter.writeEndDocument();
                String[] bankResult = sendToBank(xmlTextWriter.toXmlString(), opname);
                if (bankResult[0].length() == 0) {
                    StringBuilder tmpSbMsg = new StringBuilder();
                    XmlTextReader xmlSbReader = loadBankRespXmlReader(bankResult[1], tmpSbMsg);
                    if (xmlSbReader != null) {
                        String rsp_no = xmlSbReader.getFirstText("/msg/comm_head/rsp_no");
                        String rsp_msg = xmlSbReader.getFirstText("/msg/comm_head/rsp_msg");
                        if ("AAAAAAA".equals(rsp_no) || "SUCCESS".equals(rsp_no)) {
                            detail.respCode = "0";
                            detail.respInfo = xmlSbReader.getFirstText("/msg/main_data/SHIBYY");
                            detail.reqSeqID = xmlSbReader.getFirstText("/msg/main_data/QANTLS");
                            detail.payInfoCode = xmlSbReader.getFirstText("/msg/main_data/JIOYZT");
                            detail.transState = detail.payInfoCode;
                        }else if("YQ00999".equals(rsp_no)){
                            if(rsp_msg.equals("CMS:查询信息为空")){
                                detail.respCode = "0";
                                detail.respInfo = rsp_msg;
                                detail.reqSeqID = xmlSbReader.getFirstText("/msg/main_data/QANTLS");
                                detail.transState = rsp_no;
                                detail.payInfoCode = detail.transState;
                            }else{
                                detail.respCode = "-1";
                                detail.respInfo =rsp_no+" : " +rsp_msg;
                            }
                        } else {
                            String respCode = getBankConfig().getMapValueByKey("codeMapping",rsp_no);
                            if("0".equals(respCode)){
                                detail.respCode = "0";
                                detail.respInfo = rsp_msg;
                                detail.reqSeqID = xmlSbReader.getFirstText("/msg/main_data/QANTLS");
                                detail.transState = rsp_no;
                                detail.payInfoCode = detail.transState;
                            }else{
                                detail.respCode = "-1";
                                detail.respInfo = rsp_no+" : "+rsp_msg;
                            }
                        }
                    } else {
                        detail.respCode = "-1";
                        detail.respInfo = tmpSbMsg.toString();
                    }
                } else {
                    detail.respCode = "-1";
                    detail.respInfo = bankResult[1];
                }
            }
        }catch (Exception e){
            detail.respCode = "-1";
            detail.respInfo = e.getMessage();
            e.printStackTrace();
        }
        retAtsSingle.writeRetDetail(detail);
        return retAtsSingle;
    }

    @Override
    public RetAtsSingleBat d_1944BAT_QrySglBatPay(ReqQrySingleBat p_req1944BAT) throws Exception {
        return null;
    }

    public RetAtsSingle payBetweenSameBanks(ReqSingle p_req1908) throws Exception{
        RetAtsSingle retAtsSingle = new RetAtsSingle(p_req1908.reqHeader);
        RetAtsSglDetail detail = new RetAtsSglDetail();
        String opname = "YQ265";
        try{
            XmlTextWriter xmlTextWriter = new XmlTextWriter();
            GZRCB01Header header = new GZRCB01Header();
            header.chnl_dt = Format.Date8Format.format(p_req1908.singleDetail.getPayDateTime());
            header.chnl_tm =Format.Time6Format.format(p_req1908.singleDetail.getPayDateTime());
            header.chnl_seq =p_req1908.singleDetail.askNumber;
            wirteCommenHeader(xmlTextWriter,header);
            XElement main_data = new XElement("main_data","");
            main_data.addChild(new XElement("USERID",bankFront.loginName));
            main_data.addChild(new XElement("FUKZHH",p_req1908.singleDetail.payAct));
            main_data.addChild(new XElement("FUKHUM",p_req1908.singleDetail.payName));
            main_data.addChild(new XElement("SHKZHH",p_req1908.singleDetail.recAct));
            main_data.addChild(new XElement("SHKHUM",p_req1908.singleDetail.recName));
            main_data.addChild(new XElement("JIOYJE",p_req1908.singleDetail.payAmount));
            String CHUIBZ ="";//钞汇标志，人民币一定是0
            if("1".equals(p_req1908.singleDetail.payCur)) {
                CHUIBZ = "0";//钞户
            }
            main_data.addChild(new XElement("CHUIBZ",CHUIBZ));
            main_data.addChild(new XElement("HUOBDH",convertCurToBank(p_req1908.singleDetail.payCur)));
            main_data.addChild(new XElement("ZHYODM",p_req1908.singleDetail.purpose));
            main_data.addChild(new XElement("BEIY01",""));
            main_data.addChild(new XElement("BEIY02",""));
            String plain = bankFront.loginName+"|"
                    +p_req1908.singleDetail.payAct+"|"+p_req1908.singleDetail.recAct+"|"+p_req1908.singleDetail.payAmount;
            main_data.addChild(new XElement("SIGDAT",plain));
            nodeAnalysis(p_req1908.singleDetail.srcXElement,main_data,getCharset());
            xmlTextWriter.writeXElement(main_data);
            xmlTextWriter.writeEndDocument();
            String[] bankResult = sendToBank(xmlTextWriter.toXmlString(),opname);
            if(bankResult[0].length()==0){
                StringBuilder tmpSbMsg = new StringBuilder();
                XmlTextReader xmlSbReader = loadBankRespXmlReader(bankResult[1],tmpSbMsg);
                if(xmlSbReader!=null){
                        String rsp_no = xmlSbReader.getFirstText("/msg/comm_head/rsp_no");
                        String rsp_msg = xmlSbReader.getFirstText("/msg/comm_head/rsp_msg");
                        detail.respCode = "0";
                        detail.respInfo = rsp_msg;
                        detail.reqSeqID = xmlSbReader.getFirstText("/msg/comm_head/chnl_seq");
                        detail.payInfoCode = rsp_no;
                        detail.transState = rsp_no;
                        detail.transAmt = xmlSbReader.getFirstText("/msg/main_data/JIOYJE");
                        detail.outTransID = xmlSbReader.getFirstText("/msg/comm_head/host_seq");
                }else {
                    detail.respCode ="-1";
                    detail.respInfo = tmpSbMsg.toString();
                }
            }else if (bankResult[0].equals(ConnState.FAIL)) {
                detail.respCode = "0";
                detail.respInfo = bankResult[1];
                detail.reqSeqID = p_req1908.singleDetail.askNumber;
                detail.transState = PaymentState.FAIL_Payment;
                detail.payInfoCode = bankResult[0];
            } else{
                detail.respCode ="-1";
                detail.respInfo = bankResult[1];
            }
        }catch (Exception e){
            detail.respCode = "-1";
            detail.respInfo = e.getMessage();
            e.printStackTrace();
        }
        retAtsSingle.writeRetDetail(detail);
        return retAtsSingle;
    }

    public RetAtsSingle payBetweenDifferenceBanks(ReqSingle p_req1908) throws Exception{
        RetAtsSingle retAtsSingle = new RetAtsSingle(p_req1908.reqHeader);
        RetAtsSglDetail detail = new RetAtsSglDetail();
        String opname = "YQ266";
        try{
            XmlTextWriter xmlTextWriter = new XmlTextWriter();
            GZRCB01Header header = new GZRCB01Header();
            header.chnl_dt = Format.Date8Format.format(p_req1908.singleDetail.getPayDateTime());
            header.chnl_tm =Format.Time6Format.format(p_req1908.singleDetail.getPayDateTime());
            header.chnl_seq =p_req1908.singleDetail.askNumber;
            wirteCommenHeader(xmlTextWriter,header);
            XElement main_data = new XElement("main_data","");
            main_data.addChild(new XElement("USERID",bankFront.loginName));//用户号
            main_data.addChild(new XElement("FUKZHH",p_req1908.singleDetail.payAct));//付款账户
            main_data.addChild(new XElement("FUKHUM",p_req1908.singleDetail.payName));//付款账户名称
            main_data.addChild(new XElement("SHKZHH",p_req1908.singleDetail.recAct));//收款账户
            main_data.addChild(new XElement("SHKHUM",p_req1908.singleDetail.recName));//收款账户名称
            //填了收款行行号支付失败，暂时设置为空
            main_data.addChild(new XElement("SHKHHO",p_req1908.singleDetail.cnapsCode));//收款行行号
            main_data.addChild(new XElement("SHKHMC",p_req1908.singleDetail.cnapsName));//收款行名称
            main_data.addChild(new XElement("JIOYJE",p_req1908.singleDetail.payAmount));//交易金额
            main_data.addChild(new XElement("HUOBDH",convertCurToBank(p_req1908.singleDetail.payCur)));//币种
            main_data.addChild(new XElement("YONGTU",p_req1908.singleDetail.purpose));//附言
            String sameCity ="";
            if("1".equals(p_req1908.singleDetail.sameCity)){
                sameCity = "0";//同城
            }else if("0".equals(p_req1908.singleDetail.sameCity)){
                sameCity = "1";//异地
            }
            main_data.addChild(new XElement("TCYDBZ",sameCity));//同城异地
            //必填字段
            if(com.fingard.text.StringHelper.hasAnyChar(p_req1908.singleDetail.paySpd)) {
                main_data.addChild(new XElement("JIAJBZ", p_req1908.singleDetail.paySpd));//加急标志
            }else{
                main_data.addChild(new XElement("JIAJBZ","0"));
            }
            main_data.addChild(new XElement("BEIY01",""));//备用字段1
            main_data.addChild(new XElement("BEIY02",""));//备用字段2
            String plain = bankFront.loginName+"|"
                    +p_req1908.singleDetail.payAct+"|"+p_req1908.singleDetail.recAct+"|"+p_req1908.singleDetail.cnapsCode+"|"+p_req1908.singleDetail.payAmount;
            main_data.addChild(new XElement("SIGDAT",plain));//收款账户
            nodeAnalysis(p_req1908.singleDetail.srcXElement,main_data,getCharset());
            xmlTextWriter.writeXElement(main_data);
            xmlTextWriter.writeEndDocument();
            String[] bankResult = sendToBank(xmlTextWriter.toXmlString(),opname);
            if(bankResult[0].length()==0){
                StringBuilder tmpSbMsg = new StringBuilder();
                XmlTextReader xmlSbReader = loadBankRespXmlReader(bankResult[1].trim(),tmpSbMsg);
                if(xmlSbReader!=null){
                    String rsp_no = xmlSbReader.getFirstText("/msg/comm_head/rsp_no");
                    String rsp_msg = xmlSbReader.getFirstText("/msg/comm_head/rsp_msg");
                    detail.respCode = "0";
                    detail.respInfo = rsp_msg;
                    detail.reqSeqID = xmlSbReader.getFirstText("/msg/comm_head/chnl_seq");
                    detail.payInfoCode = rsp_no;
                    detail.transState = rsp_no;
                    detail.transAmt = xmlSbReader.getFirstText("/msg/main_data/JIOYJE");
                    detail.outTransID = xmlSbReader.getFirstText("/msg/comm_head/host_seq");
                }else if (bankResult[0].equals(ConnState.FAIL)) {
                    detail.respCode = "0";
                    detail.respInfo = bankResult[1];
                    detail.reqSeqID = p_req1908.singleDetail.askNumber;
                    detail.transState = PaymentState.FAIL_Payment;
                    detail.payInfoCode = bankResult[0];
                } else {
                    detail.respCode ="-1";
                    detail.respInfo = tmpSbMsg.toString();
                }
            }else{
                detail.respCode ="-1";
                detail.respInfo = bankResult[1];
            }
        }catch (Exception e){
            detail.respCode = "-1";
            detail.respInfo = e.getMessage();
            e.printStackTrace();
        }
        retAtsSingle.writeRetDetail(detail);
        return retAtsSingle;
    }
}
